<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.itaoke.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:  Caption <Caption@163.com> QQ 1674653
// +----------------------------------------------------------------------
// + 用于爱淘客 的定时更新优惠券数据是否有效 itaoke.ORG

namespace Home\Controller;
use Common\Controller\FirstendController;

class AutocollectController extends FirstendController {

	public function _initialize() {
		parent :: _initialize();
		$this -> _cate_mod = D('ItemsCate');
	
		$api_config = M('ItemsSite')->where(array('code' => 'itaoke'))->getField('config');
		$this->_tbconfig = unserialize($api_config);
	
	}
	
	public function index(){
		$auth=I('auth','');

		if($auth!=C('itk_auto_auth'))
			return;
		
		// 开始刷新

		if(C('itk_auto_collect',null,0) ==1 )
			$this->autoCollect();
	}
	
	private function autoCollect(){
		$min=C('itk_auto_min');		// 自动采集最小佣金
		$pages=C('itk_auto_pages',null,1);	// 自动采集页数

		for($i=1;$i<=$pages;$i++){
			$isAuth=$this->trueAutoCollect($min, $pages);
			if($isAuth==-1)
				break;
		}
	}
	
	private function trueAutoCollect($min,$page){
		
		$tb_top = $this->_get_top();
		$req = $tb_top->load_api('ItaokeQuanGetRequest');
		$req->putOtherTextParam('cat',0);				// 设置采集数据分类ID
		$req->putOtherTextParam('page',$page);									// 设置页数
		$req->putOtherTextParam('min',$min);				// 设置最小佣金
		$resp = (array)$tb_top->execute($req);
		if($resp['status'] != '0000')
		{
			return -1;
		}
		else
		{
			$item = $resp['data'];
			$totalnum = count($item);
			for($i=0;$i<$totalnum;$i++)
			{
			$_item['cate_id'] = $item[$i]['cid'];
			$_item['num_iid'] = $item[$i]['num_iid'];
			$_item['title'] = $item[$i]['title'];
			$_item['tags'] = $item[$i]['tags'];
			$_item['sellerId'] = $item[$i]['sellerid'];
			$_item['pic_url'] = $item[$i]['pic_url'];
			$_item['price'] = $item[$i]['price'] != '' ? $item[$i]['price'] :$item[$i]['coupon_price'];
			$_item['volume'] = $item[$i]['sale_num'] == '' ? 0 : $item[$i]['sale_num'];
			$_item['coupon_price'] = $item[$i]['coupon_price'];
			$_item['coupon_rate']	= round(($_item['coupon_price']/$_item['price'])*10000, 0);
			$_item['shop_type'] = $item[$i]['source_id'] == 1 ? 'C':'B';
			$_item['item_url'] = $item[$i]['item_url'];
			$_item['add_time'] = time();
			$_item['isq'] = 1;
			$_item['quan'] = $item[$i]['qprice'];
					$_item['quanurl'] = $item[$i]['qwurl'];
					$_item['quan_surplus'] = $item[$i]['qsnum'];
					$_item['quan_receive'] = $item[$i]['qnum'] - $item[$i]['qsnum'];
					$_item['commission_rate'] = $item[$i]['commission'];
					$_item['nick']='爱淘客联盟';
					$_item['coupon_start_time']=time();
					$_item['coupon_end_time']=$item[$i]['qtime']+24*60*60-1;
					$_item['ems'] = $item[$i]['ems'];
					$_item['astime'] = date('Ymd');
					$_item['dx'] = $item[$i]['plan_id'] == 2 ? 1 :0;
					$_item['intro'] = $item[$i]['content'] ;
			$this->_ajax_tb_publish_insert($_item);
			}
		}
	}

	private function _ajax_tb_publish_insert($item) {
		return D('Items')->ajax_itk_publish($item);
	}
	
	private function _get_top() {
		vendor('Itaoke.TopClient');
		vendor('Itaoke.RequestCheckUtil');
		vendor('Itaoke.Logger');
		$top = new \TopClient;
		$top->appkey = $this->_tbconfig['app_key'];
		$top->secretKey = $this->_tbconfig['app_secret'];
		return $top;
	}
}